<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .min-box{
            position: relative;
            top: 0;
            left: 0;
            margin-top: 100px;
            margin-left: 100px;
            width: 450px;
            height: 450px;
            border: solid 1px red;
        }
        .min-box,.max-box{
            float: left;
        }
        .max-box{
            overflow: hidden;
            display: none;
            width: 600px;
            height: 600px;
            border: solid 1px rgb(97, 81, 190);
        }
        .max-img{
            position: relative;
            top: 0;
            left: 0;
        }
        .min-img{
            position: absolute;
            top: 0;
            left: 0;
        }
        .mask{
            position: absolute;
            top: 0;
            left: 0;
            width: 200px;
            height: 200px;
            /* alpha透明度 */
            background-color: rgba(0, 0, 0, 0.2);
        }
    </style>
</head>
<body>
    <div class="min-box">
        <img src="./小图.jpg" alt="" class="min-img">
        <!-- 设置蒙板 -->
        <div class="mask"></div>
    </div>
    <div class="max-box">
        <img src="大图.jpg" alt="" class="max-img">
    </div>
</body>
<script>
    var minBox=document.getElementsByClassName('min-box')[0];
    var maxBox=document.getElementsByClassName('max-box')[0];
    var minImg=document.getElementsByClassName('min-img')[0];
    var maxImg=document.getElementsByClassName('max-img')[0];
    var mask=document.getElementsByClassName('mask')[0];
    
    /* 
    1.鼠标悬停蒙板和大盒子显示
    2.蒙版跟随鼠标移动
    3.大图片跟随蒙版移动
    4.鼠标离开蒙版和大盒子隐藏 */

    // 1.鼠标悬停蒙板和大盒子显示
    minBox.onmouseover=function(e){
        mask.style.display='block';
        maxBox.style.display='block';

        //2.蒙版跟随鼠标移动
        document.onmousemove=function(e){
            //获取鼠标在盒子中的坐标
            //x轴
            var x=e.pageX-minBox.offsetLeft;
            //y轴
            var y=e.pageY-minBox.offsetTop;

            //光标在浏览器的位置=在盒子中的位置-自身一半
            var maskY=y-mask.offsetHeight/2;
            var maskX=x-mask.offsetWidth/2;
            console.log(maskX);

            //获取最大偏移量
            var maxTop=minBox.offsetHeight-mask.offsetHeight;
            var maxLeft=minBox.offsetWidth-mask.offsetWidth;
            if(maskX<0){
                maskX=0;
            }
            if(maskY<0){
                maskY=0;
            }
            if(maskX>maxLeft){
                maskX=maxLeft;
            }
            if(maskY>maxTop){
                maskY=maxTop;
            }
            mask.style.top=maskY+'px';
            mask.style.left=maskX+'px';

            // 3.大图片跟随蒙版移动
            maxImg.style.top=maskY/maxTop*(maxBox.offsetHeight-maxImg.offsetHeight)+'px';
            maxImg.style.left=maskX/maxLeft*(maxBox.offsetWidth-maxImg.offsetWidth)+'px';
        }
    }
    //4.鼠标离开蒙版和大盒子隐藏
    minBox.onmouseout=function(e){
        mask.style.display='none';
        maxBox.style.display='none';
    }
</script>
</html>